//
//  PersonalCenterViewController.m
//  ShopProject
//
//  Created by 小焘无量 on 2018/9/27.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "PersonalCenterViewController.h"
#import "OO_WebViewController.h"
#import "LoginViewController.h"
#import "ChangePasswordViewController.h"
#import "OO_AdressMenageViewController.h"
@interface PersonalCenterViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *userTelLabel;

@end

@implementation PersonalCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loginOutUIConfig];

    [self dataConfig];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// 退出UI
- (void)loginOutUIConfig {
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"";
    backItem.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
    
    UIButton *loginOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginOutBtn.frame = CGRectMake(0, 0, 60, 40);
    loginOutBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [loginOutBtn setTitle:@"退出" forState:UIControlStateNormal];
    [loginOutBtn addTarget:self action:@selector(loginOutBtnPress)  forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithCustomView:loginOutBtn];
    
    self.navigationItem.rightBarButtonItem = barBtn;
}

- (void)loginOutBtnPress {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否确定要退出" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:[LoginViewController new]];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.rootViewController = nav;
        [UserDefaults writeToPlist:@"" withKey:LOGIN_STATUS_TEL];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

/// 数据获取
-(void)dataConfig {
    JQFMDB *dataBase = [JQFMDB shareDatabase];
    NSString *userTel = [UserDefaults readFromPlist:LOGIN_STATUS_TEL];
    NSArray *arr = [dataBase jq_lookupTable:USERINFOTABLE dicOrModel:[OO_UserInfoModel class] whereFormat:@"where phone = %@",userTel];
    OO_UserInfoModel *model = arr.firstObject;
    _userNameLabel.text = model.userName;
    _userTelLabel.text = model.phone;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}

-(CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellContentViewWidth:(CGFloat)width tableView:(UITableView *)tableView {
    if (indexPath.row == 0) {
        return 120;
    }
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 1) {
        [self MyOrder];             //我的订单
    }else if (indexPath.row == 2) {
        [self myAddress];           //我的地址
    }else if (indexPath.row == 3) {
        [self changePassword];      //修改密码
    }else if (indexPath.row == 4) {
        [self aboutUsMethod];       //关于我们
    }else if (indexPath.row == 5) {
        [self callCustomerService]; //客服电话
    }
}



/// 我的订单
-(void)MyOrder {
    NSLog(@"我的订单");
}

/// 我的地址
-(void)myAddress {
    NSLog(@"我的地址");
    OO_AdressMenageViewController *addVc = [OO_AdressMenageViewController new];
    [self.navigationController pushViewController:addVc animated:1];
}

/// 修改密码
-(void)changePassword {
    ChangePasswordViewController *vc = [ChangePasswordViewController new];
    [self.navigationController pushViewController:vc animated:YES ];
}

/// 关于我们
-(void)aboutUsMethod {
    OO_WebViewController *vc  = [OO_WebViewController new];
    vc.title = @"关于我们";
    vc.imageName = @"关于我们";
    [self.navigationController pushViewController:vc animated:1];
}

/// 客服电话
-(void)callCustomerService {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",Customer_Service];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}




@end
